/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-07-01 15:40:52
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:33:47
 */
import { Component, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MaterialsServiceProxy, MaterialsEditContentDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
@Component({
    selector: 'tableAddOrEdit',
    templateUrl: './table-add-or-edit.component.html',
    styleUrls: ['./table-add-or-edit.component.less']
})
export class TableAddOrEditComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    @Input() parentInfo: any;  //树节点数据
    saving = false;
    active = false;
    form = {
        materialCode: '',
        materialName: '',
        model: '',
        materialType: '',
        materialCategoryId: '',
        unit: '',
        isUsed: true,
        materialProperty: undefined,
        // isHinge: false,
        // isPip: false,
        remark: '',
        batch: ''
    }
    materialTypeOption = [
        {
            label: '请选择',
            value: ''
        }
    ];
    materialCategoryOption = [
        {
            label: '请选择',
            value: ''
        }
    ]
    unitOption = [
        {
            label: '请选择',
            value: ''
        }
    ]
    materialPropertyOption = [
        {
            label: '请选择',
            value: ''
        }, {
            label: '整装',
            value: 0
        },
        {
            label: '芯级',
            value: 4
        },
        {
            label: '部件',
            value: 1
        }, {
            label: '组件',
            value: 2
        }, {
            label: '零件',
            value: 3
        },
    ];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _materialsServiceProxy: MaterialsServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
    }
    onChange(event) {
        //物料细类
        this._materialsServiceProxy.getSegmentationMaterialsType(this.form.materialType)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialCategoryOption = [
                    {
                        label: '请选择',
                        value: ''
                    }
                ]
                for (const item of result) {
                    this.materialCategoryOption.push(item);
                }
            });
    }
    //保存树结构
    save(): void {
        //获取实体
        let input = new MaterialsEditContentDto();
        input.materialNo = this.form.materialCode;
        input.materialName = this.form.materialName;
        input.model = this.form.model;
        input.materialsNodeId = this.form.materialType;
        input.unitId = this.form.unit;
        input.materialProperty = this.form.materialProperty;
        input.materialCategoryId = this.form.materialCategoryId;
        input.batch = this.form.batch;
        if (this.editData) {
            input.materialTypeCode = this.editData.materialsTypeCode;
        }
        if (this.form.isUsed) {
            input.isUsed = 1;
        } else {
            input.isUsed = 0;
        }
        if (this.title == '修改物料') {
            input.id = this.editData.id;
        } else {
            input.id = '00000000-0000-0000-0000-000000000000';
        }
        input.remark = this.form.remark;
        if (this.saveFlag) {
            this.saveFlag = false;
            this._materialsServiceProxy.createOrUpdateMaterialsContent(input)
                .pipe(finalize(() => { this.saving = false; }))
                .subscribe((result) => {
                    this.saveFlag = true;
                    if (result.code == 0) {
                        this._messageService.success('保存成功');
                        this.close();
                        this.modalSave.emit(true);
                    } else {
                        this._messageService.error(result.message)
                    }
                });
        }

    }
    //打开弹窗
    onShown(): void {
        this._materialsServiceProxy.getMaterialsType()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialTypeOption = [
                    {
                        label: '请选择',
                        value: ''
                    }
                ]
                for (const item of result) {
                    this.materialTypeOption.push(item);
                }
            });
        this._materialsServiceProxy.getCode('4107692e-ee97-4975-bd39-77d64354a03c')
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.unitOption = [
                    {
                        label: '请选择',
                        value: ''
                    }
                ]
                for (const item of result) {
                    this.unitOption.push(item);
                }
            });
        this._materialsServiceProxy.getSegmentationMaterialsType(this.editData.materialsTypeCode)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.materialCategoryOption = [
                    {
                        label: '请选择',
                        value: ''
                    }
                ]
                for (const item of result) {
                    this.materialCategoryOption.push(item);
                }
                if (this.title == '修改物料') {
                    this.form.materialCode = this.editData.materialNo;
                    this.form.materialName = this.editData.materialName;
                    this.form.model = this.editData.model;
                    this.form.materialType = this.editData.materialTypeId;
                    this.form.unit = this.editData.unitId;
                    this.form.batch = this.editData.batch;
                    if (this.editData.isUsed == 'True') {
                        this.form.isUsed = true;
                    } else {
                        this.form.isUsed = false;
                    }
                    this.form.remark = this.editData.remark;
                    this.form.materialProperty = this.editData.materialPropertyId;
                    if (this.editData.parentCode == 'fa568b8c-4eb5-47b1-92b1-f5aaff234de4') {
                        this.form.materialType = this.editData.materialTypeId;
                    } else {
                        this.form.materialType = this.editData.parentCode;
                    }
                    this.form.materialCategoryId = this.editData.materialCategoryId;
                } else {
                    if (this.editData.parentCode == 'fa568b8c-4eb5-47b1-92b1-f5aaff234de4') {
                        this.form.materialType = this.editData.materialPropertyId;
                    } else {
                        this.form.materialType = this.editData.parentCode;
                    }
                    this.form.materialType = this.editData.materialTypeId;
                }
            });

    }
    show(): void {
        this.active = true;
        this.modal.show();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.saveFlag = true;
        this.form.isUsed = true;
        this.form.materialCode = undefined;
        this.form.materialName = undefined;
        this.form.materialType = undefined;
        this.form.materialCategoryId = undefined;
        this.form.model = undefined;
        this.form.remark = undefined;
        this.form.unit = undefined;
        this.form.materialProperty = undefined;
        this.form.batch = undefined;
        this.active = false;
        this.modal.hide();
    }

}
